﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Xml;
using System.IO;

namespace Main.Model.Helper
{
    public class TempXMLHelper
    {
        public static void CreateFolderAndFileXML(string PathFolder, string filename)
        {
            try
            {
                //chưa có folder, tạo folder
                if (!Directory.Exists(PathFolder))
                    Directory.CreateDirectory(PathFolder);
                //tạo sẵn file xml
                XmlTextWriter writer = new XmlTextWriter(PathFolder + "/" + filename, System.Text.Encoding.UTF8);
                writer.WriteStartDocument(true);
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 2;
                writer.Close();
            }
            catch { }
        }
        //sava file xml into XMLFiles folder
        public static bool SaveTemporayDataAsXml(string xmlDataFile, DataSet ds)
        {
            bool result = false;
            try
            {
                ds.WriteXml(xmlDataFile, XmlWriteMode.WriteSchema);
                result = true;
            }
            catch { }
            return result;
        }
        // đọc file xml , tra ve dataset
        public static DataSet ReadTemporayDataFromXml(string xmlDataFile)
        {
            DataSet ds = new DataSet();
            try
            {
                foreach (DataTable dataTable in ds.Tables)
                    dataTable.BeginLoadData();

                ds.ReadXml(xmlDataFile, XmlReadMode.ReadSchema);

                foreach (DataTable dataTable in ds.Tables)
                    dataTable.EndLoadData();
            }
            catch { }
            return ds;
        }
    }
}